package com.hdoit.ares.logical.insu.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.insu.dao.IInsuranceDao;
import com.hdoit.ares.logical.insu.model.Insurance;
import com.hdoit.ares.logical.insu.model.InsuranceInsurancePeople;
import com.hdoit.ares.logical.insu.service.IInsuranceInsurancePeopleService;
import com.hdoit.ares.logical.insu.service.IInsuranceService;
import com.hdoit.ares.logical.viewmodel.insu.FindInsu;
import com.hdoit.ares.logical.viewmodel.insu.InsuranceSearch;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.logical.setting.service.IParamOptionService;
import com.tennetcn.common.logical.viewmodel.setting.ParamOptionSearch;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
/**
 * @author      lvsi
 * @email       
 * @createtime  2017年12月09日 19:10:36
 * @comment
 */
@Component
public class InsuranceServiceImpl extends SuperService<Insurance> implements IInsuranceService{

	private IParamOptionService paramOptionService;
	private IWithdrawParamService withdrawParamService;
	
	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}
	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	public IParamOptionService getParamOptionService() {
		return paramOptionService;
	}
	
	@Resource
	public void setParamOptionService(IParamOptionService paramOptionService) {
		this.paramOptionService = paramOptionService;
	}
	
	private IInsuranceDao insuranceDao;
	
	public IInsuranceDao getInsuranceDao() {
		return insuranceDao;
	}

	@Resource
	public void setInsuranceDao(IInsuranceDao insuranceDao) {
		this.insuranceDao = insuranceDao;
	}
	
	private IInsuranceInsurancePeopleService insuranceInsurancePeopleService;

	public IInsuranceInsurancePeopleService getInsuranceInsurancePeopleService() {
		return insuranceInsurancePeopleService;
	}

	@Resource
	public void setInsuranceInsurancePeopleService(IInsuranceInsurancePeopleService insuranceInsurancePeopleService) {
		this.insuranceInsurancePeopleService = insuranceInsurancePeopleService;
	}
 
	@Override
	public List<Insurance> queryInsuranceList(InsuranceSearch insuranceSearch, PagerModel pagerModel) {
		
		return insuranceDao.queryInsuranceList(insuranceSearch, pagerModel);
	}

	@Override
	public int queryInsuranceCount(InsuranceSearch insuranceSearch) {
		
		return insuranceDao.queryInsuranceCount(insuranceSearch);
	}


	
	@Override
	public boolean applyChange(Insurance insurance) {
		if(super.applyChange(insurance)){
			
			if(ModelStatus.add.equals(insurance.getModelStatus())){
				String peopleIds  = insurance.getPeopleId();
				if(!StringUtils.isEmpty(peopleIds)){
					List<InsuranceInsurancePeople> insuranceInsurancePeoples = new ArrayList<InsuranceInsurancePeople>();
					String[] peopleIdStrs = peopleIds.split(",");
					for(String peopleId : peopleIdStrs){
						InsuranceInsurancePeople insuranceInsurancePeople = new InsuranceInsurancePeople();
						insuranceInsurancePeople.setId(CommonUtils.getKeyId());
						insuranceInsurancePeople.setInsuranceId(insurance.getId());
						insuranceInsurancePeople.setInsurancePeopleId(peopleId);
						insuranceInsurancePeople.setDeleteMark(YesOrNoInteger.no);
						
						insuranceInsurancePeoples.add(insuranceInsurancePeople);
					}
					return insuranceInsurancePeopleService.insertListEx(insuranceInsurancePeoples) == insuranceInsurancePeoples.size();
						
				}
				
			}else if(ModelStatus.update.equals(insurance.getModelStatus())){
				insuranceInsurancePeopleService.deleteInsurancePeople(insurance.getId());
				String peopleIds  = insurance.getPeopleId();
				if(!StringUtils.isEmpty(peopleIds)){
					List<InsuranceInsurancePeople> insuranceInsurancePeoples = new ArrayList<InsuranceInsurancePeople>();
					String[] peopleIdStrs = peopleIds.split(",");
					for(String peopleId : peopleIdStrs){
						InsuranceInsurancePeople insuranceInsurancePeople = new InsuranceInsurancePeople();
						insuranceInsurancePeople.setId(CommonUtils.getKeyId());
						insuranceInsurancePeople.setInsuranceId(insurance.getId());
						insuranceInsurancePeople.setInsurancePeopleId(peopleId);
						insuranceInsurancePeople.setDeleteMark(YesOrNoInteger.no);
						
						insuranceInsurancePeoples.add(insuranceInsurancePeople);
					}
					return insuranceInsurancePeopleService.insertListEx(insuranceInsurancePeoples) == insuranceInsurancePeoples.size();
						
				}
			}
			
			return true;
	}
		
		return false;
	}

	@Override
	public List<ParamOption> queryList(String parentId) {
		ParamOptionSearch search = new ParamOptionSearch();
		search.setParentId(parentId);
		return paramOptionService.queryList(search);
	}

	@Override
	public List<Insurance> queryListByFindInsu(FindInsu findInsu) {
		WithdrawParam pageSize = withdrawParamService.queryModelByHelpCode("pageSize");
		findInsu.setPageSize(pageSize.getParam());
		return insuranceDao.queryListByFindInsu(findInsu);
	
}

	@Override
	public int updateUndercarriage(InsuranceSearch insuranceSearch) {
		
		return insuranceDao.updateUndercarriage(insuranceSearch);
	}

	@Override
	public int updateShelf(InsuranceSearch insuranceSearch) {
		
		return insuranceDao.updateShelf(insuranceSearch);
	}
}